<?php

namespace App\Services\Home;


use App\Models\Metro;
use App\Models\MetroStation;
use App\Models\Region;

class RegionService
{
    //    递归获取区域id
    public function getIds($id)
    {
        $menus = Region::with(['allChilds' => function ($query) {
            $query->select('id','parent_id');
        }])->where('id',$id)->get(['id']);
        $arr = [$menus[0]['id']];
        foreach ($menus[0]['allChilds'] as $v){
            $arr[] = $v['id'];
        }
        return $arr;
    }
    // 获取单个城市
    public function getCityInfo($id)
    {
        return Region::findOrFail($id);
    }
    // 获取城市和地铁线数据
    public function getCityAndMetroById($region_id)
    {
        $seo = Region::find($region_id,['seo_title','seo_keywords','seo_description']);
        $city = Region::where('parent_id',$region_id)->get();
        $metro = Metro::where('region_id',$region_id)->get();
        return response()->json([
            'code' => 0,
            'city' => $city,
            'metro' => $metro,
            'seo_info' => $seo
        ]);
    }
    // 获取区域
    public function getAreaList($region_id)
    {
        $area = Region::where('parent_id',$region_id)->get();
        return response()->json([
            'code' => 0,
            'area' => $area
        ]);
    }

    // 获取站点
    public function getMetroStation($metro_id)
    {
        $station = MetroStation::where('metro_id',$metro_id)->get();
        return response()->json([
            'code' => 0,
            'station' => $station
        ]);
    }

}